#include <amxmodx>
#include <amxmisc>
#include <newbandef>
#include <regex>

stock registerCvarsFromFile(const cfgFile[]){
	new line[NB_STR_LEN]
	new lineL[NB_STR_LEN]
	new lineR[NB_STR_LEN]
	
	new nextLine = 0
	new search = 1
	new readLen
	while(search == 1){
		nextLine = read_file(cfgFile, nextLine, line, NB_STR_LEN - 1, readLen)
		if(readLen >0){
			if(strfind(line,";")<0){
				strtok(line, lineL, NB_STR_LEN -1, lineR, NB_STR_LEN - 1, '=', 1)
				register_cvar(lineL, lineR)
				log_amx("Registering cvar %s with default value '%s'", lineL, lineR)
			}
		}
		if(nextLine == 0)
			search = 0
	}
}
stock clientPrintc(index, const text[], any:...)
{
    new szMsg[128];
    vformat(szMsg, sizeof(szMsg) - 1, text, 3);
    
    replace_all(szMsg, sizeof(szMsg) - 1, "!g", "^x04");
    replace_all(szMsg, sizeof(szMsg) - 1, "!n", "^x01");
    replace_all(szMsg, sizeof(szMsg) - 1, "!t", "^x03");
    
    message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, index);
    write_byte(index);
    write_string(szMsg);
    message_end();
}  

stock fillStringArray(const fileName[], Array:arr, maxItems, &itemsAdded){
	new line[NB_STR_LEN]
	new search = 1
	new i = 0
	new nextLine = 0
	new readLen
	while(search == 1){
		nextLine = read_file(fileName, nextLine, line, NB_STR_LEN - 1, readLen)
		if(readLen >0){
			if(strfind(line,";")<0){
				ArrayPushString(arr, line)
				i++
				if( maxItems > 0 && i>= maxItems)
					search = 0
			}
		}
		if(nextLine == 0)
			search = 0
	}
	itemsAdded = i
}
stock fillNumArray(const fileName[], Array:arr, maxItems, &itemsAdded){
	new line[NB_STR_LEN]
	new search = 1
	new i = 0
	new nextLine = 0
	new readLen
	while(search == 1){
		nextLine = read_file(fileName, nextLine, line, NB_STR_LEN -1 , readLen)
		if(readLen >0){
			if(strfind(line,";")<0){
				if(is_str_num(line)){
					ArrayPushCell(arr, str_to_num(line))
					i++
					if( maxItems > 0 && i>= maxItems)
						search = 0
				}
			}
		}
		if(nextLine == 0)
			search = 0
	}
	itemsAdded = i
}
stock fillArray(const fileName[], Array:arr, const leftStr[], maxItems, &itemsAdded, convertToNum = 0, splitToken = '='){
	new line[NB_STR_LEN]
	new lineL[NB_STR_LEN]
	new lineR[NB_STR_LEN]
	new search = 1
	new i = 0
	new nextLine = 0
	new readLen
	
	while(search == 1){
		nextLine = read_file(fileName, nextLine, line, NB_STR_LEN - 1, readLen)
		if(readLen >0){
			if(strfind(line,";") != 0){
				strtok(line, lineL, NB_STR_LEN - 1, lineR, NB_STR_LEN -1, splitToken, 1)
				if(equali(lineL, leftStr)) {
					if(convertToNum)
					{
						if(is_str_num(lineR)){
							ArrayPushCell(arr, str_to_num(lineR))
							i++
						}
					} else {
						ArrayPushString(arr, lineR)
						i++
					}
					
					if( maxItems > 0 && i>= maxItems)
						search = 0
				}
			}
		}
		if(nextLine == 0)
			search = 0
	}
	itemsAdded = i
}

stock isStrIp(const string[]){
	new ret
	new Error[NB_ERR_LEN]
	new Regex:rx = regex_match(string, "(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])", ret, Error, NB_IP_LEN)
	if(rx)
		regex_free(rx)
	return ret
}
stock isValidSteam(const string[]){
	if (equali(string, "STEAM_ID_PENDING") || equali(string, "STEAM_ID_LAN") || equali(string, "HLTV") ||
		equali(string, "4294967295") || equali(string, "VALVE_ID_LAN") || equali(string, "VALVE_ID_PENDING")){
		return 0
	}
	new ret
	new Error[NB_ERR_LEN]
	new Regex:rx = 
	regex_match(string, "STEAM_\d:(\d):(\d+)", ret, Error, NB_STEAM_LEN)
	if(rx)
		regex_free(rx)
	return ret	
}
stock ArrayContainsNum(Array:arr, num){
	new i
	for (i=0; i< ArraySize(arr); i++){
		if(ArrayGetCell(arr, i) == num)
		return 1
	}
	return 0
}
stock logToFile2(admin, player, const info[], any:...){
	new pName[NB_NAME_LEN]
	new aName[NB_NAME_LEN]
	new aSteam[NB_STEAM_LEN]
	get_user_authid(admin,aSteam,NB_STEAM_LEN - 1)
	get_user_name(admin,aName,NB_NAME_LEN - 1)
	if(player && is_user_connected(player))
		get_user_authid(player,pName,NB_NAME_LEN - 1)
	new newInfo[NB_STR_LEN]
	
	vformat(newInfo, NB_STR_LEN -1, info, 4)
	logToFile(aName, aSteam,pName, newInfo)
}
stock logToFile(const aName[], const aSteam[], const pName[], const info[], any:...){
	new newInfo[NB_STR_LEN]
	vformat(newInfo, NB_STR_LEN -1, info, 5)
	log_amx("Admin %s[%s], Player %s, Event: %s",aName, aSteam, pName, newInfo)
}
